package com.zking.controller;

import com.zking.pojo.Book;
import com.zking.pojo.Page;
import com.zking.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@ResponseBody
@RequestMapping("/books")
public class BookController {
   /* @RequestMapping("/login")
    public String login(String userName,String password){
        return "{'result':true}";
    }*/


    @Autowired
    private BookService bookService;

   // @RequestMapping(method = RequestMethod.POST)
   @PostMapping
    public Result add(@RequestBody Book book){
       System.out.println("-----------add");
       boolean flag= bookService.add(book);
       return new Result(flag,flag?Code.ADD_OK:Code.ADD_ERR,"新增");
    }
    //其4567890-=
    @PutMapping
    public Result update(@RequestBody Book book){
        System.out.println("-----------update");
        boolean flag= bookService.update(book);
        return new Result(flag,flag?Code.UPDATE_OK:Code.UPDATE_ERR,"修改");
    }
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        System.out.println("-----------delete");
        boolean flag= bookService.delete(id);
        return new Result(flag,flag?Code.DELETE_OK:Code.DELETE_ERR,"删除");
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        System.out.println("-----------getById");
       Book book= bookService.getById(id);
       return new Result(book,book!=null?Code.SELECT_OK:Code.SELECT_ERR,"根据编号查询");
    }

   /* @GetMapping("/selectByName")
    public Result selectByWhere(@RequestParam(value = "bname",required = false) String name,@RequestParam("currentPage")Integer currentPage,@RequestParam("pageSize")Integer pageSize){
        System.out.println("-----------selectByWhere");
        List<Book> list=bookService.selectBywhere(name,currentPage,pageSize);
        return new Result(list,list!=null?Code.SELECT_OK:Code.SELECT_ERR,"根据姓名模糊查询");
    }*/

   @RequestMapping("/selectByName")
    public Result selectByWhere(Book book,Page page){
        System.out.println("-----------selectByWhere");
        List<Book> list=bookService.selectBywhere(book,page);
        return new Result(list,list!=null?Code.SELECT_OK:Code.SELECT_ERR,"根据姓名模糊查询");
    }


  /*  @GetMapping("/selectByWhere/{name}")
    public Result selectByWhere(@PathVariable String name){
       List<Book>  list = bookService.selectBywhere(name);
       System.out.println(list);
       return new Result(list,list!=null?Code.SELECT_OK:Code.SELECT_ERR,"根据图书名称查询");
    }

   @RequestMapping(value = "/selectByName",method = RequestMethod.GET)
    public Result selectByName(@RequestParam(value = "bname",required = false) String name){
       System.out.println("-----------selectByName");
        List<Book>  list = bookService.selectBywhere(name);
        System.out.println(list);
        return new Result(list,list!=null?Code.SELECT_OK:Code.SELECT_ERR,"根据图书名称查询");
    }*/

    @GetMapping
    public Result getAll(){
        System.out.println("-----------getAll");
        List<Book> list= bookService.getAll();
        return new Result(list,list!=null?Code.SELECT_OK:Code.SELECT_ERR,"查询所有");
    }
}
